import re
from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("http://192.168.0.173:8088/login")
    page.get_by_placeholder("请输入用户名").click()
    page.get_by_placeholder("请输入用户名").fill("cross")
    page.get_by_placeholder("请输入用户名").press("Tab")
    page.get_by_placeholder("请输入密码").fill("abcd1234")
    page.get_by_placeholder("请输入密码").press("Enter")
    page.get_by_role("link", name="跨链网关").click()
    page.locator("td:nth-child(7) > div > button:nth-child(2)").click()
    page.get_by_placeholder("请输入链资源ID").click()
    page.get_by_placeholder("请输入链资源ID").fill("chainmaker111")
    page.get_by_placeholder("请输入Chain ID").click()
    page.get_by_placeholder("请输入Chain ID").fill("chain1")
    page.get_by_text("公钥模式").click()
    page.get_by_role("listitem", name="证书模式").click()
    page.get_by_text("国密SM3").click()
    page.get_by_role("listitem", name="非国密SHA256").click()
    page.get_by_placeholder("请输入组织ID").click()
    page.get_by_placeholder("请输入组织ID").fill("wx.org1.com.cn")
    page.get_by_placeholder("请输入跨链合约名称").click()
    page.get_by_placeholder("请输入跨链合约名称").fill("batchContract")
    page.locator("div").filter(has_text=re.compile(r"^交易签名私钥点击上传/拖拽到此区域支持\.key格式文件$")).locator("button").click()
    page.locator("div").filter(has_text=re.compile(r"^交易签名私钥点击上传/拖拽到此区域支持\.key格式文件$")).locator("button").set_input_files("client1.sign.key")
    page.locator("div").filter(has_text=re.compile(r"^交易签名证书点击上传/拖拽到此区域支持\.crt格式文件$")).locator("button").click()
    page.locator("div").filter(has_text=re.compile(r"^交易签名证书点击上传/拖拽到此区域支持\.crt格式文件$")).locator("button").set_input_files("client1.sign.crt")
    page.locator("div").filter(has_text=re.compile(r"^点击上传/拖拽到此区域支持\.crt格式文件$")).locator("button").click()
    page.locator("div").filter(has_text=re.compile(r"^点击上传/拖拽到此区域支持\.crt格式文件$")).locator("button").set_input_files("client1.tls.crt")
    page.get_by_role("button", name="点击上传", exact=True).click()
    page.get_by_role("button", name="点击上传", exact=True).set_input_files("client1.tls.key")
    page.get_by_placeholder("RPC地址可填写IP:端口或域名").click()
    page.get_by_placeholder("RPC地址可填写IP:端口或域名").fill("http:")
    page.get_by_placeholder("RPC地址可填写IP:端口或域名").press("ControlOrMeta+a")
    page.get_by_placeholder("RPC地址可填写IP:端口或域名").fill("192.168.1.206:12301")
    page.get_by_label("是").check()
    page.get_by_placeholder("请输入TLS hostname").click()
    page.get_by_placeholder("请输入TLS hostname").fill("chainmaker.org")
    page.get_by_role("button", name="点击上传", exact=True).click()
    page.get_by_role("button", name="点击上传", exact=True).set_input_files("ca.crt")
    page.get_by_role("button", name="确定").click()
    page.get_by_placeholder("RPC地址可填写IP:端口或域名").click()
    page.get_by_role("button", name="确定").click()
    page.close()

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)
